package cn.fufeiii.bt.nomiddleware.active;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * @author FuFei
 */
@Profile(value = {"act1", "act2"})
@Component
public class ActiveRunner implements InitializingBean, ApplicationContextAware {
    private static final Logger log = LoggerFactory.getLogger(ActiveRunner.class);
    private ConfigurableApplicationContext applicationContext;
    @Autowired
    private ActiveProperty activeProperty;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = (ConfigurableApplicationContext) applicationContext;
    }

    public void afterPropertiesSet() throws Exception {
        /*
         * 对于相同的属性，其优先级从大到小为：
         * active > include
         * 在active/include中，同属性下后续的值会覆盖前面的值
         */
        Environment environment = this.applicationContext.getEnvironment();
        log.info("----------------------------------------------------------");
        log.info("ActiveProfiles = {}", (Object) environment.getActiveProfiles());
        log.info("DefaultProfiles = {}", (Object) environment.getDefaultProfiles());
        log.info("变量={}", activeProperty);
        log.info("----------------------------------------------------------");

        log.info("完成配置文件测试 结束应用程序");
        applicationContext.close();
        System.exit(0);
    }
}
